﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using OpenTK.Graphics;
using Utils;

namespace GraphicsServer
{
    public partial class FormServer : Form
    {
        public FormServer()
        {
            InitializeComponent();
            lblServer.Text = PublisherService.ReturnMachineIP().ToString();
        }

        private void BtConnectClick(object sender, EventArgs e)
        {
            int port;

            if (!int.TryParse(tbPort.Text, out port))
            {
                MessageBox.Show("Please enter a valid port into the textbox");
                tbPort.Text = "";
                return;
            }

            try
            {
                var subscriberService = new SubscriberService();
                subscriberService.StartSubscriberService(port);

                var publisherService = new PublisherService();
                publisherService.StartPublisherService(port + 1);

                subscriberService.GetUpdate += (o, ex) => tbOutput.Invoke(new Action(() => tbOutput.Text += ex.Text));

                tbOutput.Text = "Connected\r\n";
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
    }
}
